package com.party.core.service.gift.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.UUIDUtils;
import com.party.core.dao.read.gift.GiftCouponReadDao;
import com.party.core.dao.write.gift.GiftCouponWriteDao;
import com.party.core.model.gift.GiftCoupon;
import com.party.core.service.gift.IGiftCouponService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author：yifeng
 * @description：
 * @date：Created in  2017/11/28 下午 15:21
 **/
@Service
public class GiftCouponService implements IGiftCouponService {
    @Autowired
    private GiftCouponReadDao giftCouponReadDao;
    @Autowired
    private GiftCouponWriteDao giftCouponWriteDao;

    @Override
    public String insert(GiftCoupon t) {
        String uuid = UUIDUtils.generateRandomUUID();
        if (Strings.isNullOrEmpty(t.getId())) {//如果业务层没有指定主键id
            t.setId(uuid);
        }
        boolean result = giftCouponWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(GiftCoupon t) {
        return giftCouponWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return giftCouponWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return giftCouponWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return giftCouponWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return giftCouponWriteDao.batchDelete(ids);
    }

    @Override
    public GiftCoupon get(String id) {
        return giftCouponReadDao.get(id);
    }

    @Override
    public List<GiftCoupon> listPage(GiftCoupon t, Page page) {
        return giftCouponReadDao.listPage(t, page);
    }

    @Override
    public List<GiftCoupon> list(GiftCoupon t) {
        return giftCouponReadDao.listPage(t, null);
    }

    @Override
    public List<GiftCoupon> batchList(Set<String> ids, GiftCoupon t, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return giftCouponReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public GiftCoupon getUnique(GiftCoupon giftCoupon) {
        return giftCouponReadDao.getUnique(giftCoupon);
    }

    @Override
    public List<GiftCoupon> findByGiftId(String giftId) {
        GiftCoupon giftCoupon = new GiftCoupon();
        giftCoupon.setGiftId(giftId);
        return giftCouponReadDao.listPage(giftCoupon, null);
    }

    @Override
    public Long getStockByCouponId(String couponId) {
        Long salesNum = giftCouponReadDao.getStockByCouponId(couponId);
        return salesNum == null ? 0 : salesNum;
    }

    @Override
    public void deleteByGiftId(String giftId) {
        giftCouponWriteDao.deleteByGiftId(giftId);
    }

    @Override
    public void deleteByUnique(GiftCoupon giftCoupon) {
        giftCouponWriteDao.deleteByUnique(giftCoupon);
    }

    @Override
    public List<GiftCoupon> findByCouponId(String couponId) {
        GiftCoupon giftCoupon = new GiftCoupon();
        giftCoupon.setCouponId(couponId);
        return giftCouponReadDao.listPage(giftCoupon, null);
    }
}
